from bs4 import BeautifulSoup
import traceback
from crawlers.utils import fetch_webpage
from crawlers.constants import REQUEST_HEADERS, DONGCHEDI_BASE_URL

def get_hot_search_from_dongchedi():
    """从懂车帝获取热榜数据
    Returns:
        热榜数据列表，每个热榜包含序号、标题、链接
    """
    try:
        webpage = fetch_webpage(DONGCHEDI_BASE_URL, REQUEST_HEADERS)
        soup = BeautifulSoup(webpage, "html.parser")
        hot_searches = []
        # 查找包含热榜数据的 ol 元素
        hot_search_elements = soup.select("ol.jsx-4163308028 li")
        for index, li_element in enumerate(hot_search_elements, 1):
            hot_search_info = {}
            hot_search_info["order"] = index
            link_element = li_element.select_one("a")
            p_element = link_element.select_one("p")
            hot_search_info["title"] = p_element.text
            hot_search_info["url"] = f"{DONGCHEDI_BASE_URL}{link_element.get('href', '')}"
            hot_searches.append(hot_search_info)
        return hot_searches
    except Exception as e:
        print(f"懂车帝热榜数据获取失败，{e}")
        print("详细错误信息:")
        traceback.print_exc()
        raise